package com.canbot.u05.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class NetWorkUtil {

        private static final String TAG = "NetWorkUtil";

//public static boolean isNetWorkUsable(Context context){
//	   WifiManager wifiManager = (WifiManager)context. getSystemService(context.WIFI_SERVICE);  
//       WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
//       System.out.println(wifiInfo.getSSID().trim());
//       if(wifiInfo!=null && ConstUtils.ssid.equals(wifiInfo.getSSID().trim())) {
//		return true;
//	} 
//      return false;
//   }

        /**
         * 判断wifi是否连接成功
         */
        public static boolean checkNetwork(Context context) {
                ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo net = conn.getActiveNetworkInfo();
                if (net != null && net.isConnected()) {
                        return true;
                }
                return false;
        }

        /**
         * 判断wifi是否连接成功
         */
        public static boolean checkNetworkRoaming(Context context) {
                ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo net = conn.getActiveNetworkInfo();
                if (net != null && net.isRoaming()) {
                        return true;
                }
                return false;
        }

        public static boolean isNetworkRoaming(Context context) {
                ConnectivityManager connectivity = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                if (connectivity == null) {
                }
                else {
                        NetworkInfo info = connectivity.getActiveNetworkInfo();
                        if (info != null
                                && info.getType() == ConnectivityManager.TYPE_MOBILE) {
                                TelephonyManager tm = (TelephonyManager) context
                                        .getSystemService(Context.TELEPHONY_SERVICE);
                                if (tm != null && tm.isNetworkRoaming()) {
                                        return true;
                                }
                                else {
                                }
                        }
                        else {
                        }
                }
                return false;
        }

        /**
         * 判断wifi是否有用
         */
        public static boolean isWifiConnected(Context context) {
                if (context != null) {
                        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                                .getSystemService(Context.CONNECTIVITY_SERVICE);
                        NetworkInfo mWiFiNetworkInfo = mConnectivityManager
                                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                        if (mWiFiNetworkInfo != null) {
                                return mWiFiNetworkInfo.isAvailable();
                        }
                }
                return false;
        }

        /**
         * 判断wifi是否开启
         */
        public static boolean isWIFIEnabled(Context context) {
                boolean enable = false;
                WifiManager wifiManager = (WifiManager) context
                        .getSystemService(Context.WIFI_SERVICE);
                if (wifiManager.isWifiEnabled()) {
                        enable = true;
                        Log.i(TAG, "isWifiEnabled");
                }

                Log.i(TAG, "isWifiDisabled");
                return enable;
        }


        /**
         * 检查当前网络是否可用
         *
         * @param context
         * @return
         */

        public static boolean isNetworkAvailable(Context context) {
                // 获取手机所有连接管理对象（包括对wi-fi,net等连接的管理）
                ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

                if (connectivityManager == null) {
                        return false;
                }
                else {
                        // 获取NetworkInfo对象
                        NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

                        if (networkInfo != null && networkInfo.length > 0) {
                                for (int i = 0; i < networkInfo.length; i++) {
                                        System.out.println(i + "===状态===" + networkInfo[i].getState());
                                        System.out.println(i + "===类型===" + networkInfo[i].getTypeName());
                                        // 判断当前网络状态是否为连接状态
                                        if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                                                return true;
                                        }
                                }
                        }
                }
                return false;
        }

        public static final boolean ping() {

                String result = null;
                try {
                        String ip = "www.baidu.com";// ping 的地址，可以换成任何一种可靠的外网
                        Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);// ping网址3次
                        // 读取ping的内容，可以不加
                        InputStream input = p.getInputStream();
                        BufferedReader in = new BufferedReader(new InputStreamReader(input));
                        StringBuffer stringBuffer = new StringBuffer();
                        String content = "";
                        while ((content = in.readLine()) != null) {
                                stringBuffer.append(content);
                        }
                        Log.d("------ping-----", "result content : " + stringBuffer.toString());
                        // ping的状态
                        int status = p.waitFor();
                        if (status == 0) {
                                result = "success";
                                return true;
                        }
                        else {
                                result = "failed";
                        }
                }
                catch (IOException e) {
                        result = "IOException";
                }
                catch (InterruptedException e) {
                        result = "InterruptedException";
                }
                finally {
                        Log.d("----result---", "result = " + result);
                }
                return false;
        }
}
